﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace HandlerContextOptions
{
    public class GatewayRoutesOptions
    {
        #region 转发配置
        /// <summary>
        /// 下游服务路由模板
        /// </summary>
        [Required]
        public string DownstreamPathTemplate { get; set; } = "/{url}";

        /// <summary>
        /// 下游服务路由模板
        /// </summary>
        [Required]
        public string DownstreamScheme { get; set; } = "http";

        /// <summary>
        /// 下游服务的地址，如果使用LoadBalancer的话这里可以填多项
        /// </summary>
        public List<DownstreamHostAndPort> DownstreamHostAndPorts { get; set; }
        #endregion

        #region 请求配置
        /// <summary>
        /// 路由模板
        /// </summary>
        [Required]
        public string UpstreamPathTemplate { get; set; } = "/api/{url}";
        /// <summary>
        /// 上游请求http方法，可使用数组
        /// </summary>
        [Required]
        public IEnumerable<HttpMethod> UpstreamHttpMethod { get; set; }
        #endregion

        /// <summary>
        /// 负载均衡的算法
        /// </summary>
        public LoadBalancerOptions LoadBalancerOptions { get; set; }

        /// <summary>
        /// 缓存
        /// </summary>
        public FileCacheOptions FileCacheOptions { get; set; }

        /// <summary>
        /// 限流
        /// </summary>
        public RouteRateLimitOptions RateLimitOptions { get; set; }

        /// <summary>
        /// 熔断
        /// </summary>
        public QoSOptions QoSOptions { get; set; }

        //public string HttpHandlerOptions { get; set; }

        public void Ini()
        {
            this.DownstreamHostAndPorts = new List<DownstreamHostAndPort>
            {
                new DownstreamHostAndPort()
            };
            this.LoadBalancerOptions = new LoadBalancerOptions();
            this.FileCacheOptions = new FileCacheOptions();
            this.RateLimitOptions = new RouteRateLimitOptions();
            this.QoSOptions = null;
            this.UpstreamHttpMethod = new List<HandlerContextOptions.HttpMethod> {
            HandlerContextOptions.HttpMethod.GET,
            HandlerContextOptions.HttpMethod.POST
        };
        }

    }

    public class DownstreamHostAndPort
    {
        /// <summary>
        /// 地址
        /// </summary>
        [Required]
        public string Host { get; set; }
        /// <summary>
        /// 端口号
        /// </summary>
        [Required]
        [Range(10, 65535)]
        public int? Port { get; set; }
    }

    public enum HttpMethod
    {
        GET, POST, PUT, DELETE, OPTIONS
    }

    public class FileCacheOptions
    {
        /// <summary>
        /// 缓存时间
        /// </summary>
        public int? TtlSeconds { get; set; }
    }

    public class RouteRateLimitOptions
    {
        /// <summary>
        /// 包含客户端白名单的数组。这意味着该阵列中的客户端将不受速率限制的影响
        /// </summary>
        public string[] ClientWhitelist { get; set; }

        /// <summary>
        /// 是否启用端点速率限制
        /// </summary>
        public bool EnableRateLimiting { get; set; }

        /// <summary>
        /// 指定限制所适用的期间，例如1s，5m，1h，1d等。
        /// 如果在该期间内发出的请求超出限制所允许的数量，则需要等待PeriodTimespan过去，然后再发出其他请求
        /// </summary>
        public string Period { get; set; }

        /// <summary>
        /// 限制时间内，指定客户端最大请求数量
        /// </summary>
        public long? Limit { get; set; }


        /// <summary>
        /// 指定可以在一定秒数后重试
        /// </summary>
        public double? PeriodTimespan { get; set; }
    }

}
